#include "MCUInit.h"
#include "MCUConfig.h"
#include "MCULEDDriver.h"
#include "MCUROM.h"
#include "MCUIO.h"
#include "MCUVardefine.h"
#include "MCUPhaser.h"
#include "MCUUart.h"

// #if (1 == MCU_CHIP_JUSHENG)
// #include "../include.h"
// #endif

u16 ad_result[2] __attribute__((aligned(4))) = {0};
u16 CapValue = 0;

void ADCInit(short PortNum)
{
    HAL_GPIO_Config(PA, GPIO_PIN_8, GPIO_MD_AN);
    ll_adc_multi_config(LL_ADC_VSEL_AVCC, LL_ADC_CHS_18, ad_result);
    GetAdc();
    CapValue = ad_result[0];
}

void LVD_Enable(void)
{
    NVIC_InitTypeDef nvic;
    nvic.NVIC_IRQChannel = LVD_IRQn;
    nvic.NVIC_IRQChannelPriority = 1;
    nvic.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&nvic);
}

void GPIOInit()
{

	#if 1
	//
	#else
#if (1 == MCU_CHIP_JUSHENG)

    TYPE_LL_GPIO_INIT gpio_struct;
    NVIC_InitTypeDef nvic;
    nvic.NVIC_IRQChannel = TIM0_IRQn;
    nvic.NVIC_IRQChannelPriority = 1;
    nvic.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&nvic);

    nvic.NVIC_IRQChannel = TIM1_IRQn;
    nvic.NVIC_IRQChannelPriority = 1;
    nvic.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&nvic);

    TYPE_LL_GPIO_INIT gpio_init_cfg;
    TYPE_LL_TIMER_PWM_CFG pwm_cfg;
    TYPE_LL_TIMER_INIT timer_init;

    memset(&gpio_init_cfg, 0x00, sizeof(gpio_struct));
    gpio_init_cfg.gpio_pin = LL_GPIO_PIN_2;
    gpio_init_cfg.gpio_mode = LL_GPIO_MODE_AF;
    ll_gpio_init(GPIOC, &gpio_init_cfg);
    ll_gpio_pin_af_config(GPIOC, LL_GPIO_PIN_SOURCE_2, LL_GPIO_AF_1);
    memset(&pwm_cfg, 0x00, sizeof(pwm_cfg));
    memset(&timer_init, 0x00, sizeof(timer_init));
    timer_init.timer_src_sel = LL_TIMER_SRC_SYS_RISING;
    timer_init.prescaler = LL_TIMER_PSC_128;
    ll_timer_init(TIMER0, &timer_init);
    pwm_cfg.pwm_duty = 0x4;
    pwm_cfg.pwm_period = 0x64;
    ll_timer_pwm_mode_config(TIMER0, &pwm_cfg);

    //PWM Ctrl LED R
    memset(&gpio_init_cfg, 0x00, sizeof(gpio_struct));
    gpio_init_cfg.gpio_pin = LL_GPIO_PIN_3;
    gpio_init_cfg.gpio_mode = LL_GPIO_MODE_AF;
    ll_gpio_init(GPIOC, &gpio_init_cfg);
    ll_gpio_pin_af_config(GPIOC, LL_GPIO_PIN_SOURCE_3, LL_GPIO_AF_1);
    memset(&pwm_cfg, 0x00, sizeof(pwm_cfg));
    memset(&timer_init, 0x00, sizeof(timer_init));
    timer_init.timer_src_sel = LL_TIMER_SRC_SYS_RISING;
    timer_init.prescaler = LL_TIMER_PSC_128;
    ll_timer_init(TIMER1, &timer_init);
    pwm_cfg.pwm_duty = 0x4;
    pwm_cfg.pwm_period = 0x64;
    ll_timer_pwm_mode_config(TIMER1, &pwm_cfg);

    //#define USE_LVD
    //for tm1640 clk and pwm led
    memset(&gpio_struct, 0x00, sizeof(gpio_struct));
    gpio_struct.gpio_pin = LL_GPIO_PIN_1 | LL_GPIO_PIN_0;
    gpio_struct.gpio_pupd = LL_GPIO_PUPD_UP;
    gpio_struct.gpio_mode = LL_GPIO_MODE_OUT;
    gpio_struct.gpio_speed_mode = LL_GPIO_SPEED_MODE_V;
    gpio_struct.gpio_speed_level = LL_GPIO_SPEED_LEVEL_7;
    ll_gpio_init(GPIOC, &gpio_struct);
#endif
#endif
}


//3V 4.7V 2V 3.9V
//extern char TxBuffer[];
//extern char BufferLen;

#define UartInit() uart_init()

void MCUInit()
{

    HAL_TouchInit();

    ADCInit(1);

    //
    EFlashInit();

    SWD_Disable();
    
    //for charging capacity, LED L pwm, LED R pwm 
    GPIOInit();
	
    UartInit();

}
